package com.ggs.rg.ybjb.adminmodule.util;

import java.util.*;
/**
 * @Auther: Sunny
 * @Date: 2018/8/20 0020 11:24
 * @Description:
 */
public class ListPagesUitl {
    /**
     * @param list 进行分页的list
     * @param pageNo 页码
     * @param pageSize 每页显示条数
     * @return 分页后数据
     */

    public static <T> Map<String,Object> listPaging(List<T> list, Integer pageNo, Integer pageSize){
        Map<String,Object> map = new TreeMap<>();
        //记录总页数
        int pages;

        if(list == null){
            list = new ArrayList<T>();
        }
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(pageNo <= 0){
            pageNo = 1;
        }
        map.put("pageNumber",pageNo);
        //计算总页数
        pages = (list.size() % pageSize == 0) ? list.size()/pageSize :list.size()/pageSize + 1;
        map.put("pages",pages);
        int totalitems = list.size();
        map.put("OrderNumber",totalitems);
        List<T> pagingList = new ArrayList<T>();

        int totalNum = ((pageNo - 1) * pageSize) + pageSize > totalitems ? totalitems : ((pageNo - 1) * pageSize) + pageSize;
        for(int i = (pageNo-1)*pageSize; i < totalNum; i++) {
            pagingList.add(list.get(i));
            map.put("pagingCount",pagingList);
        }
        return map;
    }
}
